/************************ Newly constructed ODEAN HH tests ************************************/

clear all
use "../../data/odean_hhnew.dta", replace


capture drop dummy_td01 dummy_td261
gen dummy_td01 = 0
replace dummy_td01 = 1 if ntrades_01>0
gen dummy_td261 = 0
replace dummy_td261 = 1 if ntrades_261>0

capture drop abs_sue_2 
gen abs_sue_2 = abs(sue_2)

replace hh_income = 0 if missing(hh_income)
replace hh_gender = "U" if missing(hh_gender)
replace hh_married = "U" if missing(hh_married)

encode hh_gender, gen(gender)
encode hh_married, gen(married)

* Check for outliers; 

foreach y in evol2_winsor sind_p_ctex td_stock_ptf* td_ptf* turns_*m turn_*m{

	capture drop `y'_w
	winsor2 `y', suffix(_w) cuts(1 99)

}

* If missing td_stock_ptf (i.e., no trades), then replace with zero; 
foreach y in 01 261 _41_11{

	replace td_stock_ptf`y'_w = 0 if missing(td_stock_ptf`y'_w)

	replace td_ptf`y'_w = 0 if missing(td_ptf`y'_w)
}


* Replace missing with zero for past turnover controls; 
foreach y in turn_3m turn_6m turn_12m  turns_3m turns_6m turns_12m{

	replace `y'_w = 0 if missing(`y'_w)

}

* Log of connection; 
foreach y in sci relative_fp idegree{

	capture drop log_`y'
	gen log_`y' = log(1+`y')

}

* Log of number of stocks/assets and replace missing with zero
foreach y in ptf_nstocks ptf_nassets{

	capture drop log_`y'
	gen log_`y' = log(1+`y')
	replace log_`y' = 0 if missing(log_`y')

}

* Log number of trades in the past 3, 6, 12 month; 
foreach y of varlist ntrades* {

	capture drop log_`y'
	gen log_`y' = log(1+`y')
	replace log_`y' = 0 if missing(log_`y')

}

* Spatial network measures
foreach y in geo1 geo2 gravity{
	capture drop  log_`y'_idegree 
	capture drop log_`y'_relative_fp
	gen log_`y'_idegree = log(1+sci_`y'/hh_pop*(10^6))
	gen log_`y'_relative_fp = log(1+sci_`y'/(hh_pop*firm_pop)*(10^12))
	sum log_`y'_idegree log_`y'_relative_fp, d 
}

* Winsor relative P&L ;
foreach y of varlist rpl*_01 rpl*_261 {
	capture drop `y'_w
	winsor2 `y' if `y' !=0, suffix(_w) cuts(1 99)
	replace `y'_w = `y' if missing(`y'_w)
	replace `y'_w = 0 if missing(`y'_w)
}

foreach y of varlist pl*_01 pl*_261 {
	capture drop `y'_w
	winsor2 `y' if `y' !=0, suffix(_w) cuts(1 99)
	replace `y'_w = `y' if missing(`y'_w)
	replace `y'_w = 0 if missing(`y'_w)
}

* Scale P&L by rdq1 stock_ptf; 

foreach y of varlist pl*_01 pl*_261 {

	capture drop re`y'
	gen re`y' = `y'/rdq1_stock_ptf
	capture drop re`y'_w
	winsor2 re`y', suffix(_w) cuts(1 99)
	replace re`y'_w = 0 if missing(re`y'_w)

 }


* Total Trading cost
foreach y in 01 261 {
	capture drop repldgtw_cost_`y'
	gen repldgtw_cost_`y' = repldgtw_spread_`y' + repldgtw_commission_`y'
	capture drop repldgtw_cost_`y'_w	
	winsor2 repldgtw_cost_`y', suffix(_w) cuts(1 99)
	replace repldgtw_cost_`y'_w= 0 if missing(repldgtw_cost_`y'_w)
}



foreach y in rpl_day61_01_w rpl_day61_261_w rpldgtw_day61_01_w rpldgtw_day61_261_w{

	capture drop exp_`y'
	gen exp_`y' = exp(`y')
	sum exp_`y', d
}  

foreach y in rpl_day61_01_w rpl_day61_261_w rpldgtw_day61_01_w rpldgtw_day61_261_w{

	capture drop log_`y'
	gen log_`y' = log(1+`y')
	sum log_`y', d
} 



* Prep for the regression 
local window 12
local firmctrl abs_sue_2 logme logbm idvol ior urban sp500 retail ep2_winsor evol2_winsor_w nrdq_rk2 rptlag log_xad pop_density sind_p_ctex_w
local hhControl hh_income gender married turns_`window'm_w log_ntrades_stock_`window'm log_ptf_nstocks
local hhctrl i.hh_income i.gender i.married turns_`window'm_w log_ntrades_`window'm log_ptf_nstocks 
local cxn  log_idegree
// Toggle below fro trade frequency tests
local dep1 dummy_td01 dummy_td261 ntrades_01 ntrades_261 td_stock_ptf01_w td_stock_ptf261_w
// Toggle below for "Are trades harmful" test;
local dep2 repldgtw_01_w repldgtw_ret_01_w repldgtw_cost_01_w repldgtw_261_w  repldgtw_ret_261_w repldgtw_cost_261_w 
local depVars `dep1' `dep2'
local dummy i.yr i.qtr i.day

label variable log_idegree "RSCI"
label variable abs_sue_2 "|SUE|"
label variable rpl_day61_01_w "P&L[0,1]"
label variable rpldgtw_day61_01_w "Abnormal P&L[0,1]"
label variable rpl_day61_261_w "P&L[2,61]"
label variable rpldgtw_day61_261_w "Abnormal P&L[2,61]"

label variable repldgtw_01_w "Ab P&L[0, 1]"
label variable repldgtw_spread_01_w "Spread[0, 1]"
label variable repldgtw_ret_01_w "\$CAR[0,1]"
label variable repldgtw_commission_01_w "Commission[0,1]"
label variable repldgtw_261_w "Ab P&L[2, 61]"
label variable repldgtw_spread_261_w "Spread[2, 61]"
label variable repldgtw_ret_261_w "\$CAR[2,61]"
label variable repldgtw_commission_261_w "Commission[2,61]"


label variable repl_01_w "P&L[0, 1]"
label variable repl_spread_01_w "Spread[0, 1]"
label variable repl_ret_01_w "\$RET[0,1]"
label variable repl_commission_01_w "Commission[0,1]"
label variable repl_261_w "P&L[2, 61]"
label variable repl_spread_261_w "Spread[2, 61]"
label variable repl_ret_261_w "\$RET[2,61]"
label variable repl_commission_261_w "Commission[2,61]"

local filter (holding_12m==1 | trading_12m==1) & new_buyer == 0 & month_since_open>=12 & county != county_fips

* Trade Frequency tests using `dep1'
eststo clear
foreach x in `cxn'{

	foreach y of varlist `dep1'{

	qui{

		 eststo: reghdfe `y' `x' `firmctrl' `hhctrl' `dummy' if `filter' , absorb(permno household_number ) vce(cluster permno household_number county_fips) 
		 estadd local method "Linear", replace
	}

	}

} 

noisily: esttab,  star(* 0.1 ** 0.05 *** 0.01)  transform(@*10000) label b(3) nonote stats(method p2 r2_a N, label("Method" "Pseudo R2" "Adjusted R2" "Obs.") fmt(0 %9.3f %9.3f %9.0f)) drop(*.hh_income *.gender *.married *.yr *.qtr *.day)

* Are trades harmful tests using `dep2' 
eststo clear
foreach x in `cxn'{

	foreach y of varlist `dep2'{

	qui{

		 eststo: reghdfe `y' `x' `firmctrl' `hhctrl' `dummy' if `filter' , absorb(permno household_number ) vce(cluster permno household_number county_fips) 
		 estadd local method "Linear", replace
	}

	}

} 

noisily: esttab,  star(* 0.1 ** 0.05 *** 0.01)  transform(@*10000) label b(3) nonote stats(method p2 r2_a N, label("Method" "Pseudo R2" "Adjusted R2" "Obs.") fmt(0 %9.3f %9.3f %9.0f)) drop(*.hh_income *.gender *.married *.yr *.qtr *.day)


